package com.lv.service.weidget.picker

import android.content.Context
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.github.gzuliyujiang.wheelpicker.widget.LinkageWheelLayout
import com.lv.common.imp.OnPositionMore
import com.lv.service.R
import com.lv.service.room.category.CategoryBean
import com.lxj.xpopup.core.BottomPopupView
import com.lxj.xpopup.impl.PartShadowPopupView

/**
 * 作者： ygx
 * 创建日期：2022/10/10 10:32 AM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：
 */
class OPtionPickPopupView(
    private val mContext: Context,
    private val mTitle: String = "选择分类",
    private var mAllBeans: List<CategoryBean>? = null,
    private var mWheelPosition1: Int = 0,
    private var mWheelPosition2: Int = 0,
    private var mWheelPosition3: Int = 0,
    private val positionListener: OnPositionMore<String>? = null
) :
    PartShadowPopupView(mContext) {
//    private var mTempWheelPosition1 = 0
//    private var mTempWheelPosition2 = 0
//    private var mTempWheelPosition3 = 0

    private val name = ""
    private var viewTop: View? = null
    private var tvTitle: TextView? = null
    private var tvSubmit: TextView? = null
    private var wheelLinkage: LinkageWheelLayout? = null
//    private val mBeans = mutableListOf<BookMarksBeans>()

    override fun getImplLayoutId(): Int {
        return R.layout.option_picker_popup
    }

    override fun onCreate() {
        super.onCreate()
        viewTop = findViewById<View>(R.id.view_top)
        tvTitle = findViewById<TextView>(R.id.tv_title)
        wheelLinkage = findViewById<LinkageWheelLayout>(R.id.wheel_linkage)
        tvSubmit = findViewById<TextView>(R.id.tv_submit)
        findViewById<ImageView>(R.id.iv_close).setOnClickListener { dismiss() }
        findViewById<TextView>(R.id.tv_add).setOnClickListener {
            positionListener?.onClick(
                mWheelPosition1,
                mWheelPosition2,
                -1,
                data = null
            )
        }
        initView()
        initEvnet()
    }

    private fun initView() {
        tvTitle?.text = mTitle
//        mAllBeans?.let {
//            mBeans.clear()
//            mBeans.addAll(mAllBeans)
//        }
//        mTempWheelPosition1 = mWheelPosition1
//        mTempWheelPosition2 = mWheelPosition2
//        mTempWheelPosition3 = mWheelPosition3
        val thirdLikeProvider = ThirdLikeProvider(mAllBeans)
        wheelLinkage?.setData(thirdLikeProvider)

        wheelLinkage?.setDefaultValue(mWheelPosition1, mWheelPosition2, mWheelPosition3)
//        wheelLinkage?.setDefaultValue(mAllBeans!![mWheelPosition1].name, mAllBeans[mWheelPosition1].telSecondCategories!![mWheelPosition2].name, mAllBeans[mWheelPosition1].telSecondCategories!![mWheelPosition2].telThirdCategories!![mWheelPosition3].name)
    }

    fun UpData(p1: Int, p2: Int, p3: Int, mBeans: List<CategoryBean>?) {
        mWheelPosition1 = p1
        mWheelPosition2 = p2
        mWheelPosition3 = p3
        mAllBeans = mBeans
        val thirdLikeProvider = ThirdLikeProvider(mAllBeans)
        wheelLinkage?.setData(thirdLikeProvider)

        wheelLinkage?.setDefaultValue(mWheelPosition1, mWheelPosition2, mWheelPosition3)

    }


    private fun initEvnet() {

        viewTop?.setOnClickListener {
            dismiss()
        }
        wheelLinkage?.setOnLinkageSelectedListener { first, second, third ->
            mWheelPosition1 = wheelLinkage?.firstWheelView?.currentPosition ?: 0
            mWheelPosition2 = wheelLinkage?.secondWheelView?.currentPosition ?: 0
            mWheelPosition3 = wheelLinkage?.thirdWheelView?.currentPosition ?: 0
//            mBinding.tvSearchCategory.text =
//                "${mAllBeans!![mWheelPosition1].name}-${mAllBeans[mWheelPosition1].telSecondCategories!![mWheelPosition2].name}"
        }
        tvSubmit?.setOnClickListener {
            val item =
                "${mAllBeans!![mWheelPosition1].name}/${mAllBeans?.get(mWheelPosition1)?.telSecondCategories!![mWheelPosition2].name}/${
                    mAllBeans?.get(
                        mWheelPosition1
                    )?.telSecondCategories!![mWheelPosition2].telThirdCategories!![mWheelPosition3].name
                }"
            positionListener?.onClick(
                mWheelPosition1,
                mWheelPosition2,
                mWheelPosition3,
                data = item
            )
            dismiss()
        }
    }

    public fun setTitle(title: String) {
        tvTitle?.text = title
    }

    private fun setDismissDialog() {
        dismiss()
    }


    override fun onDestroy() {
        super.onDestroy()
    }

    override fun beforeDismiss() {
        super.beforeDismiss()


    }

}